package com.wm.youfangtong.newhouse

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.wm.youfangtong.MainActivity
import com.wm.youfangtong.newhouse.flutter.client.FlutterParam
import io.flutter.embedding.android.FlutterActivityLaunchConfigs
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class NewHouseMainActivity : FlutterFragmentActivity() {

    // 通道
    private lateinit var methodChannel: MethodChannel

    companion object {

        private const val EXTRA_INITIAL_ROUTE = "route"

        private const val EXTRA_BACKGROUND_MODE = "background_mode"

        private const val EXTRA_DESTROY_ENGINE_WITH_ACTIVITY = "destroy_engine_with_activity"

        /**
         * 构建Intent
         */
        fun buildIntent(context: Context,routeName: String): Intent {
            return Intent(context,NewHouseMainActivity::class.java)
                .putExtra(EXTRA_INITIAL_ROUTE,routeName)
                .putExtra(EXTRA_BACKGROUND_MODE, FlutterActivityLaunchConfigs.BackgroundMode.opaque.name)
                .putExtra(EXTRA_DESTROY_ENGINE_WITH_ACTIVITY, true);
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        methodChannel = MethodChannel(flutterEngine.dartExecutor,FlutterParam.channel_name)
        methodChannel.setMethodCallHandler { call, result ->
            when (call.method) {
                "toHomePage" -> {
                    startActivity(Intent(this,MainActivity::class.java))
                }
            }
        }
    }
}